<?php

namespace ctrl;

use model\admin_user;
use libs\view;
use libs\verimg;

class login
{
	private static $M, $captcha = ["登录成功", "cxuuweb_php_login"];
	public static function Init()
	{
		self::$M = new admin_user;
	}

	public static function index()
	{
		view::display();
	}

	static function login()
	{
		if (!verimg::check($_POST['captcha'], self::$captcha[1]))
			Response(['status' => 0, 'info' => '验证码错误']);

		$result = self::$M->CheckLogin();
		if (empty($result)) Response(['status' => 0, 'info' => '用户名或者密码错误']);

		if ($result['status'] == 1) {
			$promises = [
				'userid' => $result['id'],
				'gid' => $result['gid']
			];
			//$_SESSION["cxuu_admin"] = $promises;
			$keeplogin = empty($_POST['keeplogin']) ? 1 : 7; //七天保持登录
			setcookie(self::$captcha[1], Encrypt(serialize($promises), $GLOBALS['ZPHP_CONFIG']['SYSTEMNAME']), time() + 28800 + 86400 * $keeplogin);
			self::$M->loginDbInsert($result['id']);
			SqlLog(self::$captcha[0], $result['id']);
			Response(['status' => 1, 'info' => self::$captcha[0]]);
		} else {
			Response(['status' => 0, 'info' => '帐户被禁用!']);
		}
	}


	static function loginOut()
	{
		//unset($_SESSION["cxuu_admin"]);
		setcookie(self::$captcha[1], "", time() - 86400 * 7);
		Redirect(PHP_FILE);
	}

	//验证码生成函数
	//访问路径 /admin.php?c=login&a=verify
	static function verify()
	{
		$img = new verimg();
		$img->img(self::$captcha[1]);
	}
}
